package com.hub.realtime.resource;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hub.realtime.resource.domain.ResFlinkModule;
import com.hub.realtime.resource.domain.ResModuleJar;
import com.hub.realtime.resource.dto.input.FlinkProjectQueryParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hub.realtime.common.annotation.Log;
import com.hub.realtime.common.core.controller.BaseController;
import com.hub.realtime.common.core.domain.AjaxResult;
import com.hub.realtime.common.enums.BusinessType;
import com.hub.realtime.resource.domain.ResFlinkProject;
import com.hub.realtime.resource.service.IResFlinkProjectService;
import com.hub.realtime.common.utils.poi.ExcelUtil;
import com.hub.realtime.common.core.page.TableDataInfo;

/**
 * git项目管理Controller
 *
 * @author wupeng
 * @date 2022-01-09
 */
@RestController
@RequestMapping("/resource/project")
public class ResFlinkProjectController extends BaseController {
    @Autowired
    private IResFlinkProjectService resFlinkProjectService;

    /**
     * 查询git项目管理列表
     */
    @PreAuthorize("@ss.hasPermi('resource:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(FlinkProjectQueryParam param) {
        Page<ResFlinkProject> page = resFlinkProjectService.list(param);
        return getDataTable(page.getRecords(),page.getTotal());
    }


    /**
     * 导出git项目管理列表
     */
    @PreAuthorize("@ss.hasPermi('resource:project:export')")
    @Log(title = "git项目管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FlinkProjectQueryParam param) {
        List<ResFlinkProject> list = resFlinkProjectService.list(param).getRecords();
        ExcelUtil<ResFlinkProject> util = new ExcelUtil<ResFlinkProject>(ResFlinkProject.class);
        util.exportExcel(response, list, "git项目管理数据");
    }

    /**
     * 获取git项目管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('resource:project:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(resFlinkProjectService.getBaseMapper().selectById(id));
    }

    /**
     * 新增git项目管理
     */
    @PreAuthorize("@ss.hasPermi('resource:project:add')")
    @Log(title = "git项目管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ResFlinkProject resFlinkProject) {
        return toAjax(resFlinkProjectService.saveInfo(resFlinkProject));
    }

    /**
     * 修改git项目管理
     */
    @PreAuthorize("@ss.hasPermi('resource:project:edit')")
    @Log(title = "git项目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ResFlinkProject resFlinkProject) {
        return toAjax(resFlinkProjectService.saveInfo(resFlinkProject));
    }

    /**
     * 删除git项目管理
     */
    @PreAuthorize("@ss.hasPermi('resource:project:remove')")
    @Log(title = "git项目管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(resFlinkProjectService.deleteInfos(ids));
    }


    @PutMapping(value = "/build/{id}")
    public AjaxResult build(@PathVariable("id") Long id) {
        resFlinkProjectService.build(id);
        return AjaxResult.success("构建成功!");
    }

    @PutMapping(value = "/closebuild/{id}")
    public AjaxResult closebuild(@PathVariable("id") Long id) {
        resFlinkProjectService.closeBuildLog(id);
        return AjaxResult.success("取消成功！!");
    }

    @GetMapping(value = "/seeBuildLog/{id}")
    public AjaxResult seeBuildLog(@PathVariable("id") Long id) {
        resFlinkProjectService.seeBuildLog(id);
        return AjaxResult.success("查看成功!");
    }

    @GetMapping("/getGitProjectList")
    public TableDataInfo getGitProjectList() {
        List<ResFlinkProject> list = resFlinkProjectService.getGitProjectList();
        return getDataTable(list,null);
    }


    /**
     * 获取模块儿信息
     * @return
     */
    @GetMapping("/getModuleList")
    public TableDataInfo getModuleList() {
        List<ResFlinkModule> list = resFlinkProjectService.getModulList();
        return getDataTable(list,null);
    }

    @GetMapping("/getModuleJarList")
    public TableDataInfo getModuleJarList() {
        List<ResModuleJar> list = resFlinkProjectService.getModuleJarList();
        return getDataTable(list,null);
    }

}

